import { contractListUrl } from '../../../../api/individual'
import { post } from '../../../../utils/index'

Page({
  data: {
    active: 0,
    tabList: [
      { id: 0, title: '待签字', list: [], finished: false, params: { current: 1, size: 10, sceneStatus: 0 } },
      { id: 1, title: '已签字', list: [], finished: false, params: { current: 1, size: 10, sceneStatus: 1 } },
      { id: 2, title: '主动放弃', list: [], finished: false, params: { current: 1, size: 10, sceneStatus: 2 } }
    ],
    bgList: ['blue-color-bg', 'yellow-color-bg', 'green-color-bg', 'pink-color-bg', 'purple-color-bg']
  },
  async loadData () { // 获取合同列表
    const { tabList, active } = this.data
    let tab = tabList[active]
    if (tab.finished) {
      return
    }
    let { data: { records, total } } = await post(contractListUrl, tab.params, {}, false)
    tab.list = tab.list.concat(records)
    tab.params.current++
    if (tab.list.length >= total) {
      tab.finished = true
    }
    this.setData({ tabList })
  },
  onPullDownRefresh () { // 上拉刷新
    const { tabList, active } = this.data
    let tab = tabList[active]
    tab.list = []
    tab.finished = false
    tab.params.current = 1
    this.setData({ tabList })
    this.loadData()
  },
  onReachBottom () { // 下拉加载更多
    this.loadData()
  },
  tabChange ({ detail }) { // 切换tab
    const { index } = detail
    this.setData({ active: index })
    this.loadData()
  },
  contract ({ currentTarget }) { // 签署合同
    const { index } = currentTarget.dataset
    const { tabList, active } = this.data
    const list = tabList[active].list
    const item = list[index]
    const typeList = ['转岗', '续签', '延长合同', '离职']
    const type = typeList.findIndex(type => type === item.contractSceneCategory)
    wx.navigateTo({ url: `/pages/webview/webview?url=/dispatch-sign/#/contract&id=${item.id}&status=${item.sceneStatus}&type=${type}` })
  },
  onShow () {
    this.loadData()
  }
})